//
//  AYSSpecialDtailViewController.m
//  AiYingShiProject
//
//  Created by lanou on 15/10/27.
//  Copyright (c) 2015年 李晓理. All rights reserved.
//

#import "AYSSpecialDtailViewController.h"
#import "AYSSpecialDetailCell.h"
#import "AYSRequestManager.h"
#import "AYSSpecialDetialModel.h"
#import "AYSBaseNavigantonViewController.h"
#import "AYSCommonPlayViewController.h"
#import "MJRefresh.h"
#import "SVProgressHUD.h"
@interface AYSSpecialDtailViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSourceArray;  // 数据数组
@property(nonatomic, assign)NSInteger pageindex;
@end

@implementation AYSSpecialDtailViewController

// 懒加载
- (NSMutableArray *)dataSourceArray
{
    if (!_dataSourceArray)
    {
        self.dataSourceArray = [NSMutableArray array];
    }
    return _dataSourceArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [SVProgressHUD show];
     NSString *url = [NSString stringWithFormat:@"http://doukantv.com/api/top/?cli=iphone&pageindex=1&ver=2.3.4&sys_ver=9.1&pagesize=20&topic=%@&type=movie",self.movieID];
    [self requestDataWithUrl:url];
    [SVProgressHUD dismiss];
    //[self.navigationController setNavigationBarHidden:YES];
   // [self.tabBarController.tabBar setHidden:YES];
    
    self.titleLabel.text = self.mainModel.programname;
       
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,65, SCREEN_WIDTH, SCREEN_HEIGHT - 65) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor  = [UIColor colorWithWhite:0.044 alpha:0.940];
    _tableView.rowHeight = (SCREEN_HEIGHT - 100) / 4 + 10;
    [self.view addSubview:_tableView];
    
    // 注册cell
    [_tableView registerClass:[AYSSpecialDetailCell class] forCellReuseIdentifier:@"detail"];
    [self refreshData];
}

// 数据请求
- (void)requestDataWithUrl:(NSString *)url
{
     [AYSRequestManager requestWithType:GET url:url parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSArray *arr = dic[@"result"];
        for ( NSDictionary *dict in arr)
        {
            AYSSpecialDetialModel *model = [[AYSSpecialDetialModel alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            [self.dataSourceArray addObject:model];
        }
        [_tableView reloadData];
    } error:^(NSError *error) {
        NSLog(@"专题详情页面数据请求失败");
    }];
    
}

#pragma wark ================刷新
- (void)refreshData
{
    _pageindex = 1;
    __weak UITableView *tableView = self.tableView;
    // 下拉刷新
    tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        NSString *url = [NSString stringWithFormat:@"http://doukantv.com/api/top/?cli=iphone&pageindex=1&ver=2.3.4&sys_ver=9.1&pagesize=20&topic=%@&type=movie",self.movieID];
        [self requestDataWithUrl:url];
        [tableView.header endRefreshing];
        
    }];
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    tableView.header.automaticallyChangeAlpha = YES;
    
    // 上拉刷新
    tableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
    _pageindex ++;
    NSString *url = [NSString stringWithFormat:@"http://doukantv.com/api/top/?cli=iphone&pageindex=%ld&ver=2.3.4&sys_ver=9.1&pagesize=20&topic=%@&type=movie",_pageindex,self.movieID];
    [self requestDataWithUrl:url];
    [self.tableView reloadData];
    [tableView.footer endRefreshing];
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AYSSpecialDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detail" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithWhite:0.081 alpha:0.920];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.detailModel = _dataSourceArray[indexPath.row];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AYSCommonPlayViewController *commonVC = [[AYSCommonPlayViewController alloc] init];
    AYSSpecialDetialModel *model = _dataSourceArray[indexPath.row];
    commonVC.programeID = model.programID;
    [self.navigationController pushViewController:commonVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
